package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.QuickMarkRecord;
import com.ruoyi.system.mapper.QuickMarkRecordMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysUserInviteMapper;
import com.ruoyi.system.domain.SysUserInvite;
import com.ruoyi.system.service.ISysUserInviteService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 邀请用户审批Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-15
 */
@Service
public class SysUserInviteServiceImpl implements ISysUserInviteService
{
    @Autowired
    private SysUserInviteMapper sysUserInviteMapper;
    @Autowired
    private QuickMarkRecordMapper quickMarkRecordMapper;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询邀请用户审批
     *
     * @param id 邀请用户审批主键
     * @return 邀请用户审批
     */
    @Override
    public SysUserInvite selectSysUserInviteById(Long id)
    {
        return sysUserInviteMapper.selectSysUserInviteById(id);
    }

    /**
     * 查询邀请用户审批列表
     *
     * @param sysUserInvite 邀请用户审批
     * @return 邀请用户审批
     */
    @Override
    public List<SysUserInvite> selectSysUserInviteList(SysUserInvite sysUserInvite)
    {
        List<SysUserInvite> sysUserInviteList = sysUserInviteMapper.selectSysUserInviteList(sysUserInvite);
        for(SysUserInvite invite : sysUserInviteList){
            Long quickMarkId = invite.getQuickMarkId();
            QuickMarkRecord quickMarkRecord = quickMarkRecordMapper.selectQuickMarkRecordById(quickMarkId);
            invite.setIsApprove(quickMarkRecord.getIsApprove());
        }
        return sysUserInviteList;
    }

    /**
     * 新增邀请用户审批
     *
     * @param sysUserInvite 邀请用户审批
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSysUserInvite(SysUserInvite sysUserInvite)
    {
        sysUserInvite.setCreateTime(DateUtils.getNowDate());
        String isApprove = sysUserInvite.getIsApprove();
        if("1".equals(isApprove)){
            //不需要审批直接添加用户
            SysUser user = new SysUser();
            BeanUtils.copyProperties(user,sysUserInvite);
            sysUserService.insertUser(user);
            sysUserInvite.setApproveStatus("3");
        }
        return sysUserInviteMapper.insertSysUserInvite(sysUserInvite);
    }

    /**
     * 修改邀请用户审批
     *
     * @param sysUserInvite 邀请用户审批
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSysUserInvite(SysUserInvite sysUserInvite)
    {
        sysUserInvite.setUpdateTime(DateUtils.getNowDate());
        String approveStatus = sysUserInvite.getApproveStatus();
        if("1".equals(approveStatus)){
            SysUser user = new SysUser();
            BeanUtils.copyProperties(user,sysUserInvite);
            sysUserService.insertUser(user);
        }
        return sysUserInviteMapper.updateSysUserInvite(sysUserInvite);
    }
}
